package com.lisx.module_note.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.db.NoteDbEntity;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.manager.NoteManager;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.module_route.NoteModuleRoute;
import com.lisx.module_note.adapter.NoteListAdapter;
import com.lisx.module_note.databinding.ActivityNoteListBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@Route(path = NoteModuleRoute.NOTE_LIST_ACTIVITY)
public class NoteListActivity extends BaseActivity {

    private NoteListAdapter adapter;
    private ActivityNoteListBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        binding = ActivityNoteListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NoteListActivity.this, WriteNoteActivity.class));
            }
        });
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new NoteListAdapter();
        binding.recyclerview.setAdapter(adapter);

        adapter.setOnItemClickListener(new NoteListAdapter.OnItemClickListener() {
            @Override
            public void onEdit(NoteDbEntity noteDbEntity) {
                Intent intent = new Intent(NoteListActivity.this, WriteNoteActivity.class);
                intent.putExtra("note", noteDbEntity);
                startActivity(intent);
            }

            @Override
            public void onDelete(NoteDbEntity noteDbEntity) {
                deleteNote(noteDbEntity);
            }
        });

        setAdapterData();
    }

    private void deleteNote(NoteDbEntity noteDbEntity) {
        Observable.create(new ObservableOnSubscribe<List<NoteDbEntity>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<NoteDbEntity>> emitter) throws Exception {
                List<NoteDbEntity> noteData = NoteManager.getInstance().deleteNoteDbEntity(noteDbEntity);
                emitter.onNext(noteData);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<NoteDbEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<NoteDbEntity> noteDbEntities) {
                        if (noteDbEntities != null && noteDbEntities.size() > 0) {
                            binding.noDataContainer.setVisibility(View.GONE);
                            binding.recyclerview.setVisibility(View.VISIBLE);
                            adapter.setData(noteDbEntities);
                        } else {
                            binding.noDataContainer.setVisibility(View.VISIBLE);
                            binding.recyclerview.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void setAdapterData() {
        Observable.create(new ObservableOnSubscribe<List<NoteDbEntity>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<NoteDbEntity>> emitter) throws Exception {
                List<NoteDbEntity> noteData = NoteManager.getInstance().getNoteData();
                emitter.onNext(noteData);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<NoteDbEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<NoteDbEntity> noteDbEntities) {
                        if (noteDbEntities != null && noteDbEntities.size() > 0) {
                            binding.noDataContainer.setVisibility(View.GONE);
                            binding.recyclerview.setVisibility(View.VISIBLE);
                            adapter.setData(noteDbEntities);
                        } else {
                            binding.noDataContainer.setVisibility(View.VISIBLE);
                            binding.recyclerview.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(EventEntity eventBusEntity) {
        if (eventBusEntity.getCode() == EventBusConstants.UPDATE_NOTE) {
            setAdapterData();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
